package com.xjcs.saplingplan.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.View
import com.xjcs.saplingplan.R

class RoundBorderView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var cornerRadius: Float = 0f
    private var topLeftRadius: Float = 0f
    private var topRightRadius: Float = 0f
    private var bottomLeftRadius: Float = 0f
    private var bottomRightRadius: Float = 0f

    private var strokeWidth: Int = 0
    private var strokeColor: Int = Color.TRANSPARENT
    private var bgColor: Int = Color.TRANSPARENT

    // 阴影相关
    private var shadowEnabled: Boolean = false
    private var shadowColor: Int = Color.parseColor("#40000000") // 默认半透明黑
    private var shadowRadius: Float = 4f // dp
    private var shadowDx: Float = 0f
    private var shadowDy: Float = 2f

    init {
        val typedArray = context.obtainStyledAttributes(
            attrs,
            R.styleable.RoundBorderView,
            defStyleAttr,
            0
        )

        try {
            cornerRadius = typedArray.getDimension(R.styleable.RoundBorderView_cornerRadius, 0f)
            topLeftRadius = typedArray.getDimension(R.styleable.RoundBorderView_topLeftRadius, cornerRadius)
            topRightRadius = typedArray.getDimension(R.styleable.RoundBorderView_topRightRadius, cornerRadius)
            bottomLeftRadius = typedArray.getDimension(R.styleable.RoundBorderView_bottomLeftRadius, cornerRadius)
            bottomRightRadius = typedArray.getDimension(R.styleable.RoundBorderView_bottomRightRadius, cornerRadius)

            strokeWidth = typedArray.getDimensionPixelSize(R.styleable.RoundBorderView_strokeWidth, 0)
            strokeColor = typedArray.getColor(R.styleable.RoundBorderView_strokeColor, Color.TRANSPARENT)
            bgColor = typedArray.getColor(R.styleable.RoundBorderView_bgColor, Color.TRANSPARENT)

            // 阴影属性
            shadowEnabled = typedArray.getBoolean(R.styleable.RoundBorderView_hasShadow, false)
            shadowColor = typedArray.getColor(R.styleable.RoundBorderView_shadowColor, Color.parseColor("#40000000"))
            shadowRadius = typedArray.getDimension(R.styleable.RoundBorderView_shadowRadius, 4f)
            shadowDx = typedArray.getDimension(R.styleable.RoundBorderView_shadowDx, 0f)
            shadowDy = typedArray.getDimension(R.styleable.RoundBorderView_shadowDy, 2f)

        } finally {
            typedArray.recycle()
        }

        updateBackground()
        applyShadow()
    }

    private fun updateBackground() {
        val drawable = GradientDrawable().apply {
            shape = GradientDrawable.RECTANGLE

            // 判断是否使用统一圆角
            if (topLeftRadius == cornerRadius &&
                topRightRadius == cornerRadius &&
                bottomLeftRadius == cornerRadius &&
                bottomRightRadius == cornerRadius
            ) {
                this.cornerRadius = cornerRadius
            } else {
                cornerRadii = floatArrayOf(
                    topLeftRadius, topLeftRadius,
                    topRightRadius, topRightRadius,
                    bottomRightRadius, bottomRightRadius,
                    bottomLeftRadius, bottomLeftRadius
                )
            }

            setStroke(strokeWidth, strokeColor)
            setColor(bgColor)
        }

        background = drawable
    }

    private fun applyShadow() {
        if (shadowEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 启用系统 elevation 阴影
            elevation = shadowRadius
            translationZ = 0f
            // 注意：系统阴影的形状由 Outline 决定，而 Outline 默认跟随 background 的轮廓
            // 所以只要你的 background 是圆角的，阴影就是圆角的
        } else {
            // 低版本 Android 或者想用自定义阴影，可以：
            // 1. 使用 padding 模拟（不推荐，只是视觉偏移）
            // 2. 使用自定义绘制（onDraw 中绘制阴影）
            // 3. 使用父布局的 CardView 包裹（推荐最简单实用）
            //
            // 这里暂时什么都不做，或者你可以设置一个带偏移的背景
            // 或者提示用户使用 CardView
        }
    }

    // 提供给外部调用的方法
    fun setCornerRadius(radius: Float) {
        cornerRadius = radius
        topLeftRadius = radius
        topRightRadius = radius
        bottomLeftRadius = radius
        bottomRightRadius = radius
        updateBackground()
    }

    fun setCornerRadii(
        topLeft: Float,
        topRight: Float,
        bottomLeft: Float,
        bottomRight: Float
    ) {
        topLeftRadius = topLeft
        topRightRadius = topRight
        bottomLeftRadius = bottomLeft
        bottomRightRadius = bottomRight
        updateBackground()
    }

    fun setStrokeWidth(width: Int) {
        strokeWidth = width
        updateBackground()
    }

    fun setStrokeColor(color: Int) {
        strokeColor = color
        updateBackground()
    }

    fun setBgColor(color: Int) {
        bgColor = color
        updateBackground()
    }

    fun setShadowEnabled(enabled: Boolean) {
        shadowEnabled = enabled
        applyShadow()
    }

    fun setShadowParams(radius: Float, dx: Float, dy: Float, color: Int) {
        shadowRadius = radius
        shadowDx = dx
        shadowDy = dy
        shadowColor = color
        applyShadow()
    }
}